﻿Imports System.Web.UI
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.Administration.Views
Imports SISSAT.WebDevelopment.Views

Namespace [Shared].UserControls
    Public Class WebUserDetailControl
        Inherits UserControl
        Implements IWebUserDetailControlView

#Region "Fields"

        Private _controlMode As PageModes

#End Region

#Region "Properties"

        Public Property ErrorMessage As String
        Public Property UseControlModeFromUserInterface As Boolean
        Public Property PasswordLabel As String

        Public Property ControlMode As PageModes
            Get
                Return _controlMode
            End Get
            Set(value As PageModes)
                If UseControlModeFromUserInterface Then
                    Exit Property
                End If

                _controlMode = value
                IsReadOnly = Not (_controlMode = PageModes.EditMode)
                CssClass = Convert.ToString(IIf(IsReadOnly, "textbox readonly", "textbox req"))

                InitializeComponents()
            End Set
        End Property

        Public Property ActiveViewIndex As Integer
            Get
                Return ButtonViews.ActiveViewIndex
            End Get
            Set(value As Integer)
                ButtonViews.ActiveViewIndex = value
            End Set
        End Property

        Private Property Presenter As WebUserDetailControlPresenter
        Private Property CssClass As String
        Private Property IsReadOnly As Boolean

#End Region

#Region "IWebUserDetailControlView Members"

        Public ReadOnly Property CurrentUserId As Integer Implements IWebUserDetailControlView.CurrentUserId
            Get
                Return SISSATPageBase.User.Employee.EmployeeId
            End Get
        End Property

        Public WriteOnly Property DropDownListDataSource As IEnumerable(Of KeyValuePair(Of String, String)) Implements IWebUserDetailControlView.DropDownListDataSource
            Set(value As IEnumerable(Of KeyValuePair(Of String, String)))
                DropDownListControl.DataSource = value
            End Set
        End Property

        Public ReadOnly Property EmployeeId As Integer Implements IWebUserDetailControlView.EmployeeId
            Get
                Dim id As Integer = 0
                Dim eid As String = HttpContext.Current.Request.QueryString("eid")
                If String.IsNullOrEmpty(eid) Then
                    id = Convert.ToInt32(DropDownListControl.SelectedControlValue)
                ElseIf Not String.IsNullOrEmpty(eid) AndAlso Integer.TryParse(eid, id) Then
                    ' No logic
                End If

                Return id
            End Get
        End Property

        Public Event OnClear() Implements IWebUserDetailControlView.OnClear
        Public Event OnInitialization() Implements IWebUserDetailControlView.OnInitialization
        Public Event OnSubmit() Implements IWebUserDetailControlView.OnSubmit

        Public Property NewPassword As String Implements IWebUserDetailControlView.NewPassword
            Get
                Return PasswordNewTextbox.Text
            End Get
            Set(ByVal value As String)
                PasswordNewTextbox.Text = value
            End Set
        End Property

        Public Property OldPassword As String Implements IWebUserDetailControlView.OldPassword
            Get
                Return PasswordTextBox.Text
            End Get
            Set(ByVal value As String)
                PasswordTextBox.Text = value
            End Set
        End Property

        Public ReadOnly Property UserId As Integer Implements IWebUserDetailControlView.UserId
            Get
                Dim id As Integer = 0
                Dim uid As String = HttpContext.Current.Request.QueryString("uid")

                If String.IsNullOrEmpty(uid) Then
                    ControlMode = PageModes.EditPartiallyMode
                    id = SISSATPageBase.User.UserId
                ElseIf String.Compare(uid, "new", True) = 0 OrElse Integer.TryParse(uid, id) Then
                    ControlMode = SISSATPageBase.AppropriatePageModeForUser()
                End If

                Return id
            End Get
        End Property

        Public Property Username As String Implements IWebUserDetailControlView.Username
            Get
                Return UsernameTextBox.Text
            End Get
            Set(value As String)
                UsernameTextBox.Text = value
            End Set
        End Property

#End Region

#Region "Methods"

        Private Sub InitializeComponents()
            PasswordLabel = "Password"
            ActiveViewIndex = Convert.ToInt32(IIf(IsReadOnly, 0, 1))
            DropDownListControl.Visible = Not IsReadOnly
            UsernameTextBox.ReadOnly = IsReadOnly
            UsernameTextBox.CssClass = CssClass
            PasswordTextBox.ReadOnly = IsReadOnly
            PasswordTextBox.CssClass = CssClass

            If ControlMode = PageModes.EditPartiallyMode Then
                PasswordLabel = "Old Password"
                ActiveViewIndex = 1
                PasswordTextBox.ReadOnly = Not IsReadOnly
                PasswordTextBox.CssClass = "textbox req"
            End If
        End Sub

#End Region

#Region "Events"

        Private Sub PageInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
            Presenter = New WebUserDetailControlPresenter(Me)
            RaiseEvent OnInitialization()

            InitializeComponents()
        End Sub

        Private Sub SubmitButton_Click(sender As Object, e As System.EventArgs) Handles SubmitButton.Click
            Try
                RaiseEvent OnSubmit()
            Catch ex As ArgumentNullException
                Me.ErrorMessage = "Old password is incorrect."
            End Try
        End Sub

        Private Sub ClearButton_Click(sender As Object, e As System.EventArgs) Handles ClearButton.Click
            RaiseEvent OnClear()
        End Sub

#End Region

    End Class
End Namespace
